{% extends "base.html" %}
{% import "_macros.html" as toc_macros %}


{% block title %}{{ section.title }}{% endblock title %}


{% block header_logo %}
<p class="header-title">{{ config.title }} | {{ section.title }}</p>
{% endblock header_logo %}


{% block header_nav_extended %}
{% if section.ancestors | length > 0 %}
{% set parent = section.ancestors | last %}
{% set parent_path = parent | as_str %}
{% set parent_section = get_section(path=parent_path) %}
<a
  class="nav__link"
  href="{{ parent_section.permalink }}"
  aria-label="Navigation to the parent path; aka 'Back up'."
>Parent</a>
{% else%}
<a
  class="nav__link"
  href="."
  aria-label="Navigation to the parent path; aka 'Back up'."
>Parent</a>
{% endif %}
{% endblock header_nav_extended %}


{% block content %}

<!-- Description -->
{% if section.description %}
<section aria-label="Page Metadata">
  <h2>{{ section.title }} | Description</h2>
  <p>{{ section.description }}</p>
</section>
{% endif %}

<!-- Table of Contents -->
{% if section.toc %}
<nav aria-label="Table of Contents">
  <h2>Table of Contents</h2>
  <ul>
    {% if section.pages or section.subsections %}
    <!-- NOTE: All of the section nav parts are processed
         independently of the page content, so clobbering anchor ids could
         occur with the current implementation. -->
    <li><a href="#sectionContentsNoClobber">Section Contents</a></li>{% endif %}
    {{ toc_macros::render_toc(items=section.toc, max_depth=2, current_depth=1) | safe }}
  </ul>
</nav>
{% endif %}

<!-- Section Contents -->
{% set sorted_section_pages = section.pages | sort(attribute="title") %}
{% set pages_without_dates = section.pages | filter(attribute="date", value="") %}
{% set pages_with_dates = section.pages | filter(attribute="date") %}
{% set sorted_pages_with_dates = pages_with_dates | sort(attribute="date") | reverse %}
{% set sorted_section_subsections = section.subsections | sort() %}

{% if section.pages or section.subsections %}
<nav aria-label="Section Contents">
  <h2 id="sectionContentsNoClobber">Section Contents</h2>
  <ul>
    {% if sorted_section_subsections %}
    <li><a href="#subsections">Subsections</a></li>{% endif %}
    {% if sorted_pages_with_dates %}
    <li><a href="#pagesByDate">Pages, Dated</a></li>{% endif %}
    {% if pages_without_dates %}
    <li><a href="#pagesWithoutDates">Pages, Undated</a></li>{% endif %}
    {% if sorted_section_pages %}
    <li><a href="#pagesByTitle">Pages By Title</a></li>{% endif %}
  </ul>

  {% if sorted_section_subsections %}
  <h3 id="subsectionsNoClobber">Subsections</h3>
  <ul>
    <!-- List subsections -->
    {% for section_subsection in sorted_section_subsections %}
    {% set subsection = get_section(path=section_subsection) %}
    <li>
      <a href="{{ subsection.permalink | safe }}">{{ subsection.title }}</a>
    </li>
    {% endfor %}
  </ul>
  {% endif %}

  {% if sorted_pages_with_dates %}
  <h3 id="pagesByDateNoClobber">Pages, Dated</h3>
  <ul>
    <!-- List dated pages -->
    {% for page in sorted_pages_with_dates %}
    <li>
      <p>{% if page.date %}{{ page.date }} &bullet;{% endif %}
        <a href="{{ page.permalink }}">{{ page.title }}</a>
      </p>
      {% if page.description %}
      <p>{{ page.description }}</p>
      {% elif page.summary %}
      <p>{{ page.summary }}</p>
      {% else %}
      <p>{{ page.title }}....</p>
      {% endif %}
    </li>
    {% endfor %}
  </ul>
  {% endif %}

  {% if pages_without_dates %}
  <!-- NOTE: This filter for pages_without_dates tends to have none. -->
  <h3 id="pagesWithoutDatesNoClobber">Pages, Undated</h3>
  <ul>
    <!-- List undated pages -->
    {% for page in pages_without_dates %}
    <li>
      <p>{% if page.date %}{{ page.date }} &bullet;{% endif %}
        <a href="{{ page.permalink }}">{{ page.title }}</a>
      </p>
      {% if page.description %}
      <p>{{ page.description }}</p>
      {% elif page.summary %}
      <p>{{ page.summary }}</p>
      {% else %}
      <p>{{ page.title }}....</p>
      {% endif %}
    </li>
    {% endfor %}
  </ul>
  {% endif %}

  {% if sorted_section_pages %}
  <h3 id="pagesByTitleNoClobber">Pages By Title</h3>
  <ul>
    <!-- List other pages -->
    {% for page in sorted_section_pages %}
    <li>
      <p>{% if page.date %}{{ page.date }} &bullet;{% endif %}
        <a href="{{ page.permalink }}">{{ page.title }}</a>
      </p>
      {% if page.description %}
      <p>{{ page.description }}</p>
      {% elif page.summary %}
      <p>{{ page.summary }}</p>
      {% else %}
      <p>{{ page.title }}....</p>
      {% endif %}
    </li>
    {% endfor %}
  </ul>
  {% endif %}

</nav>
{% endif %}

{{ section.content | safe }}

{% endblock content %}
